package control;

import com.alibaba.fastjson.JSONObject;
import connect.BaseValue;
import connect.HttpConnect;

import java.util.HashMap;
import java.util.Map;

public class UserControl {
    UserControl(){};
    /**
      * @Description 登录
      * @return 登录成功返回token 登陆失败返回""
      * @author 孔明浩
      * @Date 2020/5/1
      */
    public String login(String username, String password){
        Map<String, String> params = new HashMap<>();
        params.put("user", username);
        params.put("passwd", password);
        HttpConnect connect = new HttpConnect();
        String result = connect.doPost(BaseValue.BASE_URL + "/login", null, params);
        JSONObject object = JSONObject.parseObject(result);
        if (object.getInteger("code") == 0){
            return object.getString("token");
        }
        return "";
    }

    public String register(String username, String password){
        Map<String, String> params = new HashMap<>();
        params.put("user", username);
        params.put("passwd", password);
        HttpConnect connect = new HttpConnect();
        String result = connect.doPost(BaseValue.BASE_URL + "/register", null, params);
        JSONObject object = JSONObject.parseObject(result);
        if (object.getInteger("code") == 0){
            return object.getString("token");
        }
        return "";
    }
}
